import json
from random import random

from bs4 import BeautifulSoup
from loguru import logger


class EnrollMentPlainParse:
    def __init__(self):
        super().__init__()

    def parse_plain_url_list(self, povince_map, university_id):
        query_list = []
        year_code_arr = range(2017, 2022)
        for povince in povince_map:
            for year_code in year_code_arr:
                # 0-100 之间的随机整数
                # random_num = int(random() * 100)
                p_code = povince_map[povince]
                item_url = f"http://www.juzhiyuan.com/index/school/ajax_major_plans?sid={university_id}&province={p_code}&year={year_code}&kemu=0"
                query_list.append({
                    "url": item_url,
                    "povince_name": povince,
                    "povince_code": p_code,
                    "year_code": year_code,
                })
        return query_list

    def parse_plain_result_list(self, json,povince_name,university_name):
        result_list = []
        data_arr = json["data"]
        for data in data_arr:
            major_fee = data["xuefei"]
            if data["xuefei"] == -1:
                major_fee = "待定"
            if data["xuefei"] == 0:
                major_fee = "免费"
            result_list.append({
                "plan_year": data["year"],
                "university_name": university_name,
                "province_name": povince_name[data["province"]],
                "plan_major_code": data["major_code"],
                "plan_category": "文科" if data["kemu"] == 1 else "理科" if data["kemu"] == 2 else "综合",
                "plan_batch": data["pici_title"],
                "plan_major_name": data["major_title"],
                "plan_count": data["plan_num"],
                # (item.xuefei == -1?'待定':(item.xuefei == 0?'免费':item.xuefei)) + ' 学年/' + (item.learn_year)
                "major_fee": str(major_fee) + " 学年/ " + str(data["learn_year"])
            })
        return result_list
